<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	订单，V8打印机：58mm（32字符），V9：80mm（42字符）
	设置字体大小、居中及切纸、来单提示音：http://www.jinyunzn.com/manage/index.php?plugin=core&action=article.show&nosession=1&id=11（出处：进云手册）
*/
class printmsg extends \plugin{
	public function storeprint($sendcontent='',$order_id='',$is_pay=0,$daike=0,$printerid=0,$pluginname='',$ordergoods=array(),$isadd=false,$print_type=0,$post=array()){
		$isprint=false;
		$params=array('uniacid'=>$_SESSION['uniacid'],'print_status'=>1);
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		if($order['merchid']){
			$params['merchid']=$order['merchid'];
		}elseif($order['storeid']){
			$params['storeid']=array(0,$order['storeid']);
		}
		if($print_type){
			$params['print_type']=$print_type;
		}else{
			$params['print_type <=']=3;
		}
		if($post['is18']){
			$params['type']=18;
			if($printerid){
				$params['id']=$printerid;
			}
			$is18=pdo_count('jy_weishop_printer',$params);
			if($is18){
				exi('','','jy_weishop/store.dayin18//id='.$order_id);
			}else{
				unset($params['type']);
			}
		}
		$order['api_data']=json_decode($order['api_data'],true);
		if(!$ordergoods){
			$ordergoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order_id));
		}
		if($ordergoods){
			foreach($ordergoods as &$ov3){
				if($order['merchid']){
					$ov3['pcate']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$ov3['goodsid']),'pcate2');
				}else{
					$ov3['pcate']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$ov3['goodsid']),'pcate');
				}
			}
		}
		$conttags=$this->model('printtpl')->upconttags($order,$is_pay,$daike,$printerid,$pluginname,$ordergoods,$isadd);
		if($printerid){
			$printers=pdo_getall('jy_weishop_printer',array('id'=>$printerid,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$printers=pdo_getall('jy_weishop_printer',$params);
		}
		foreach($printers as $p){
			$newordergoods=$ordergoods;
			if($p['goodstype']){
				if($p['goodstype']==1 && $p['goodspids']){
					//按商品分类打印
					$goods=array();
					foreach($ordergoods as $ov){
						if(strpos($p['goodspids'],';'.$ov['pcate'].';') !== false){
							$goods[]=$ov;
						}
					}
					$p['ordergoods']=$goods;
				}elseif($p['goodstype']==2){
					//指定商品打印
					$p['goods']=json_decode($p['goods'],true);
					$setgoods=';';
					foreach($p['goods'] as &$p){
						$setgoods .=$p['id'].';';
					}
					$goods=array();
					foreach($ordergoods as $ov){
						if(strpos($setgoods,';'.$ov['goodsid'].';') !== false){
							$goods[]=$ov;
						}
					}
					$p['ordergoods']=$goods;
				}
				$newordergoods=$p['ordergoods'];
			}
			if(count($newordergoods)<1){
				continue;
			}
			//单商品打印,单商品标签打印
			if($p['tpl_type']==1 || $p['tpl_type']==2 || $p['tpl_type']==3){
				$pgoods=array();
				foreach($newordergoods as $ov){
					$pgoods[0]=$ov;
					$goodsstr=$this->getgoodsstr($pgoods,$p['tpl_type'],$p['type']);
					$textinfo=array(
						'goodsinfo'=>$goodsstr['goodsinfo'],
						'goodsinfo2'=>$goodsstr['goodsinfo2'],
						'goodsinfoold'=>$goodsstr['goodsinfo'],
						'conttags'=>$conttags,
					);
					$sendcontent=$this->model('printtpl')->upcontent($order,$textinfo,$p,$pluginname,$newordergoods,$isadd);
					$this->toprint($p,$sendcontent,$order,$daike,$print_type);
				}
			}else{
				if($p['goodstype']){
					$conttags=$this->model('printtpl')->upconttags($order,$is_pay,$daike,$p['type'],$pluginname,$newordergoods,$isadd);
				}
				$goodsstr=$this->getgoodsstr($newordergoods,'',$p['type']);
				$textinfo=array(
					'goodsinfo'=>$goodsstr['goodsinfo'],
					'goodsinfo2'=>$goodsstr['goodsinfo2'],
					'goodsinfoold'=>$goodsstr['goodsinfo'],
					'conttags'=>$conttags,
				);
				$sendcontent=$this->model('printtpl')->upcontent($order,$textinfo,$p,$pluginname,$newordergoods,$isadd);
				$this->toprint($p,$sendcontent,$order,$daike,$print_type);
			}
			pdo_update('jy_weishop_order',array('isprint'=>1),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
			$isprint=true;
		}
		return $isprint;
	}
	public function getgoodsstr($ordergoods,$tpl_type=0,$ptype=2){
			$str=$strnew = '';
			foreach($ordergoods as $g){
				if(!$g['title']){
					$info=pdo_get('jy_weishop_goods',array('id'=>$g['goodsid']),array('title'));
					$g['title']=$info['title'];
				}
				if($tpl_type==2 || $tpl_type==3){
					return array('goodsinfo'=>$g);
				}
				$optiontitle=$g['optionid']?'-['.$g['optiondata'].']':'';
				if($ptype==1 || $ptype==8 || $ptype==9){
					$title0=cfc('tools')->substr($g['title'],0,9);
					$optiontitle=cfc('tools')->substr($optiontitle,0,9);
					$title0w=18;
					$moneyw=8;
					$conttags['line']=$this->strlen_pad('-',30,'-')."\n";
				}elseif($ptype==2 || $ptype==3 || $ptype==7  || $ptype==10){
					$title0=cfc('tools')->substr($g['title'],0,15);
					$optiontitle=cfc('tools')->substr($optiontitle,0,15);
					$title0w=32;
					$moneyw=11;
					$conttags['line']=$this->strlen_pad('-',47,'-')."\n";
				}
				$money = $g['price'];
				$money=price($money);
				if($g['optionid']){
					$strnew .= $this->strlen_pad($title0,$title0w)."\n";
					$strnew .= $this->strlen_pad($optiontitle,$title0w);
				}else{
					$strnew .= $this->strlen_pad($title0,$title0w);
				}
				if(!$tpl_type){
					$strnew .= $this->strlen_pad('x'.$g['total'],4);
					$strnew .= $this->strlen_pad($money,$moneyw). "\n";
				}else{
					$strnew .= $this->strlen_pad('x'.$g['total'],6);
					$strnew .= $this->strlen_pad('',$moneyw). "\n";
					
				}
			}
			if($tpl_type){
				$goodsinfoold='';
				$goodsinfo='';
			}else{
				$strtop=$this->strlen_pad('品名',$title0w);
				$strtop .= $this->strlen_pad('数量',6);
				$strtop .= '单价';
				$goodsinfo="<B>".$strtop."\n</B>";
			}
			$goodsinfo .= $strnew;
			$goodsinfo2 .= "<S2>".$strnew."</S2>";
			$goodsinfo .= $conttags['line'];
			$goodsinfo2 .= $conttags['line'];
			return array('goodsinfo2'=>$goodsinfo2,'goodsinfo'=>$goodsinfo);
	}
	public function toprint($p,$content='',$order=array(),$daike=0,$print_type=0){
			//print_r($content);exit;
			if($p['type']==7 || $p['type']==9){
				//飞蛾打印机
				$content=str_replace('\n','<BR>',$content);
				$content=str_replace('<AM><B><S2>','<CB>',$content);
				$content=str_replace('</S2></B></AM>','</CB>',$content);
				$content=str_replace('<AM>','<C>',$content);
				$content=str_replace('</AM>','</C>',$content);
				$content=str_replace('<AR>','<RIGHT>',$content);
				$content=str_replace('</AR>','</RIGHT>',$content);
				$content=str_replace('<B>','<BOLD>',$content);
				$content=str_replace('</B>','</BOLD>',$content);
				$content=str_replace('<S1>','<BOLD>',$content);
				$content=str_replace('</S1>','</BOLD>',$content);
				$content=str_replace('<S2>','<B>',$content);
				$content=str_replace('</S2>','</B>',$content);
				$content=str_replace('<S3>','<B>',$content);
				$content=str_replace('</S3>','</B>',$content);
				$content=str_replace('<S4>','<B>',$content);
				$content=str_replace('</S4>','</B>',$content);
				$content=str_replace('<S5>','<B>',$content);
				$content=str_replace('</S5>','</B>',$content);
				$content=str_replace('<S6>','<B>',$content);
				$content=str_replace('</S6>','</B>',$content);
				$content=str_replace('<S7>','<B>',$content);
				$content=str_replace('</S7>','</B>',$content);
				$content=str_replace('<S8>','<B>',$content);
				$content=str_replace('</S8>','</B>',$content);
			}elseif($p['type']==8 || $p['type']==10){
				//芯烨打印机
				$content=str_replace('\n','<BR>',$content);
				$content=str_replace('<AM>','<C>',$content);
				$content=str_replace('</AM>','</C>',$content);
				$content=str_replace('<AR>','<R>',$content);
				$content=str_replace('</AR>','</R>',$content);
				$content=str_replace('<B>','<BOLD>',$content);
				$content=str_replace('</B>','</BOLD>',$content);
				$content=str_replace('<S1>','<N>',$content);
				$content=str_replace('</S1>','</N>',$content);
				$content=str_replace('<S2>','<B>',$content);
				$content=str_replace('</S2>','</B>',$content);
				$content=str_replace('<S3>','<B>',$content);
				$content=str_replace('</S3>','</B>',$content);
				$content=str_replace('<S4>','<B2>',$content);
				$content=str_replace('</S4>','</B2>',$content);
				$content=str_replace('<S5>','<B2>',$content);
				$content=str_replace('</S5>','</B2>',$content);
				$content=str_replace('<S6>','<B2>',$content);
				$content=str_replace('</S6>','</B2>',$content);
				$content=str_replace('<S7>','<B2>',$content);
				$content=str_replace('</S7>','</B2>',$content);
				$content=str_replace('<S8>','<B2>',$content);
				$content=str_replace('</S8>','</B2>',$content);
			}
			//file_put_contents(ROOT_D.'/1.txt',$content);
			$printparams=array('sendcontent'=>$content,'id'=>$p['printerid']);
			if($print_type || $p['print_type'] == 1 || $daike || ($p['print_type']==2 && ($order['ispay'] || $order['status']==1)) || ($p['print_type']==3 && $order['status']==3) || $p['print_type']>3){//付款后打印/货到付款			
				if($p['type']==7 || $p['type']==9){
					$this->plugin('jy_feieyun')->printer($printparams,'jy_weishop',true,$p['print_nums'],$p['tpl_type']);
				}elseif($p['type']==8 || $p['type']==10){
					model('jy_xpyun/model')->sendprint($printparams['id'],$printparams['sendcontent'],true,$p['print_nums'],$p['tpl_type']);
				}else{
					$this->plugin('jy_print')->printer($printparams,'jy_weishop',true,$p['print_nums']);
				}
				$this->printer_log($p,$content,$order);
			}
			return true;
	}
	public function printer_log($p=array(),$content='',$order=array()){
		$log=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$order['mid'],
				'orderid'=>$order['id'],
				'storeid'=>$p['storeid'],
				'merchid'=>$p['merchid'],
				'printerid'=>$p['id'],
				'type'=>$p['type'],
				'print_type'=>$p['print_type'],
				'print_nums'=>$p['print_nums'],
				'goodstype'=>$p['goodstype'],
				'tpl_type'=>$p['tpl_type'],
				'content'=>$content,
				'pluginname'=>$p['pluginname'],
				'createtime'=>time()
		);
		$this->pdo_insert('printer_log',$log);
		return;
	}
	public function sendNotice($params){
		$tag = ((isset($params['tag']) ? $params['tag'] : ''));
		$touser = ((isset($params['openid']) ? $params['openid'] : ''));
		if (empty($touser)) {
			return;
		}
	}
	public function strlen_pad($str,$alllen,$zifu='')//补齐空格
	{
		//真实字符串长度（一个中文=2）
		$len = (strlen($str)+mb_strlen($str,'UTF-8'))/2;
		$buqi = '';
		for($i=0;$i<($alllen-$len);$i++){
			$buqi .= $zifu?:' ';
		}
		$new_str = $str.$buqi;
		return $new_str;
	}
}
